/**
 *
 */
package org.insia.smartposse.manager;

import org.hibernate.HibernateException;
import org.insia.smartposse.form.SkillsForm;
import org.insia.smartposse.manager.iface.IManager;
import org.insia.smartposse.mapping.FreelanceSkills;
import org.insia.smartposse.mapping.Skills;
import org.insia.smartposse.mapping.Users;
import org.insia.smartposse.mapping.dao.FreelanceSkillsDAO;

/**
 * @author cmougel
 *
 */
public class FreelanceSkillManager implements IManager {

	FreelanceSkillsDAO _dao = new FreelanceSkillsDAO();
	FreelanceSkills _skill;

	public FreelanceSkillManager(){
		_skill = new FreelanceSkills();
	}

	public FreelanceSkillManager(FreelanceSkills skill) {
		_skill = skill;
	}
	
	public FreelanceSkillManager(Users user, int skill) {
		_skill = _dao.getFRByUserSkill(user, skill, _dao.getSession());
	}
	
	public int getId() {
		return _skill.getId();
	}

	public void setId(int id) {
		_skill.setId(id);
	}

	/**
	 *
	 * @return The comment of This Skill
	 */
	public String getComment(){
		return _skill.getComment();
	}

	/**
	 * Define the comment of this Book
	 *
	 * @param comment
	 */
	public void setComment(String comment){
		_skill.setComment(comment);
	}

	/**
	 *
	 * @return The rank of this Skill
	 */
	public Integer getRanking(){
		return _skill.getRanking();
	}

	/**
	 * Define the Rank of this Skill
	 *
	 * @param rank
	 */
	public void setRanking(Integer rank){
		//TODO: New system of ranking with best re-ordering
		_skill.setRanking(rank);
	}

	/**
	 * Define the Freelance of this Skill
	 *
	 * @param freelance
	 */
	public void setFreelance(UserManager freelance){
		if (freelance.isFreelance()){
			_skill.setFreelance(freelance.getUser());
		} else {
			throw new HibernateException("The User must be a Freelance for association with a Skill");
		}
	}

	/**
	 * Define the grade of this Skill
	 *
	 * @param grade
	 */
	public void setGrade(Integer grade){
		if (grade < 1 || grade > 5){
			throw new HibernateException("The Grade must be into 1 to 5");
		}
		_skill.setGrade(grade);
	}

	/**
	 *
	 * @return the grade of this Skill
	 */
	public Integer getGrade(){
		return _skill.getGrade();
	}

	/**
	 *
	 * @return the Freelance of this Book
	 */
	public UserManager getFreelance(){
		return new UserManager(_skill.getFreelance());
	}

	/**
	 * Define the Skill
	 *
	 * @param skill
	 */
	public void setSkill(SkillManager skill){
		_skill.setSkill(skill.getSkill());
	}

	/**
	 *
	 * @return The Skill
	 */
	public SkillManager getSkill(){
		return new SkillManager(_skill.getSkill());
	}


	/* (non-Javadoc)
	 * @see org.insia.smartposse.manager.iface.IManager#save()
	 */
	public void save() {

		// No null in database
		if (null == _skill.getComment()){
			_skill.setComment("");
		}

		_dao.saveOrUpdate(_skill);
	}

	public void populateSkillForm(UserManager user, SkillManager skill, SkillsForm form) {
		setFreelance(user);
		setSkill(skill);
		setGrade(form.getGrade());
		setComment(form.getComment());
		setRanking(form.getRanking());
	}
	
	/* (non-Javadoc)
	 * @see org.insia.smartposse.manager.iface.IManager#save()
	 */
	public void delete() {
		_dao.delete(_skill);
	}
}
